package ar.edu.unrn.lia.bean.report;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

@ManagedBean(name = "reportUtilBean")
@ViewScoped
public class ReportUtilBean {

	private static String PATH = "/reports/";
	private static String RECIBO_CUOTA = "recibocuota.jasper";
	private static String RECIBO_PRE_INGRESO = "recibopreingreso.jasper";
	
	JasperPrint jasperPrint;
	private Map parameters;
	
	
	@ManagedProperty(value = "#{loginBean.nombre}")
	private String usuarioNombre; 
	
	
	

	public String getUsuarioNombre() {
		return usuarioNombre;
	}

	public void setUsuarioNombre(String usuarioNombre) {
		this.usuarioNombre = usuarioNombre;
	}

	@PostConstruct
	public void init() {
		parameters = new HashMap();
	}

	private void init(List list, String reportName) throws JRException {
		JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(
				list);
		String reportPath = FacesContext.getCurrentInstance()
				.getExternalContext().getRealPath(PATH + reportName);
		jasperPrint = JasperFillManager.fillReport(reportPath, parameters,
				beanCollectionDataSource);
	}

	private void printReport(List list, String reportName, String nombrePdf) throws JRException,
			IOException {

		init(list, reportName);
		HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext
				.getCurrentInstance().getExternalContext().getResponse();
		httpServletResponse.setHeader("Content-disposition",
				"attachement; filename="+nombrePdf);

		httpServletResponse.setContentType("application/pdf");
		ServletOutputStream servletOutputStream = httpServletResponse
				.getOutputStream();
		JasperExportManager.exportReportToPdfStream(jasperPrint,
				servletOutputStream);
		FacesContext.getCurrentInstance().responseComplete();
	}
	
	
	private StreamedContent printReportToFile(List list, String reportName, String nombrePdf) throws JRException,
	IOException {
		init(list, reportName);
		
		JasperExportManager.exportReportToPdfFile(jasperPrint,	FacesContext.getCurrentInstance().getExternalContext().getRealPath("/recibos") + nombrePdf);
		
		return  new DefaultStreamedContent(new FileInputStream(FacesContext.getCurrentInstance().getExternalContext().getRealPath("/recibos") + nombrePdf), "application/pdf", nombrePdf);  
	
	}

//	public StreamedContent printReciboCuota(List lista) {
//		parameters.put(
//				"TITULO",
//				ResourceBundle.getBundle(
//						"messages",
//						FacesContext.getCurrentInstance().getViewRoot()
//								.getLocale()).getString("report.title"));
//		parameters.put(
//				"SUBTITULO",
//				ResourceBundle.getBundle(
//						"messages",
//						FacesContext.getCurrentInstance().getViewRoot()
//								.getLocale()).getString("report.recibocuota"));
//		parameters.put(
//				"USER_LOGIN",
//				usuarioNombre);
//
//		try {
//			return printReportToFile(lista, RECIBO_CUOTA,"reciboCuota" + fechaHoy() + ".pdf");
//		} catch (JRException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			return  null;
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			return null;
//		}
//	}
//	
	public StreamedContent printReciboPreIngreso(List lista, String concepto) {
		parameters.put(
				"TITULO",
				ResourceBundle.getBundle(
						"messages",
						FacesContext.getCurrentInstance().getViewRoot()
								.getLocale()).getString("report.title"));		
		parameters.put("SUBTITULO",concepto);
		parameters.put("USER_LOGIN",usuarioNombre);

		try {
			return printReportToFile(lista, RECIBO_PRE_INGRESO,"reciboPreIngreso" + fechaHoy() + ".pdf");
		} catch (JRException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return  null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public String fechaHoy()
	{
		Calendar hoy = Calendar.getInstance();
		
		return String.valueOf(hoy.get(Calendar.DATE)) +"-" + String.valueOf(hoy.get(Calendar.MONTH))+"-" + String.valueOf(hoy.get(Calendar.YEAR));
	
		
	}

}
